package module_five.task03;

import module_five.task03.ClientModel.Student;
import module_five.task03.ClientModel.StudentMessage;
import module_five.task03.ClientModel.UserMessage;
import module_five.task03.serverModel.ServerDao;
import module_five.task03.serverModel.ServerStudentManager;

import java.io.IOException;

public class ServerView {

    private ServerInitClose sic;

    private ServerDao sd;

    private ServerStudentManager ssm;

    public ServerView(ServerInitClose sic, ServerDao sd, ServerStudentManager ssm) {
        this.sic = sic;
        this.sd = sd;
        this.ssm = ssm;
    }

    /**
     * 自定义成员方法, 实现接收客户端发送的消息并处理的功能
     *  用户发送的消息有两种: UserMessage 和 StudentMessage
     */
    public void serverReceive() throws IOException, ClassNotFoundException {

        Object obj = sic.getOis().readObject();

        if (obj instanceof UserMessage) { //如果发送的是UserMessage, 判断是否登录成功

            UserMessage um = (UserMessage) obj;
            //打印请求信息
            System.out.print("服务端收到客户端用户登录请求, 请求数据为: 账号名(" + um.getUser().getUserName());
            System.out.println(", 密码为:" + um.getUser().getPassword() + ", 调用serverManagerCheck方法验证");

            //判断用户账号密码是否正确
            StringBuilder type = new StringBuilder();
            if (sd.serverManagerCheck(um.getUser())) {
                type.append("success"); //设置type类型为success
            } else {
                type.append("fail");
            }

            um.setType(type.toString());// 设置了用户消息的type类型

            System.out.println("响应结果为:{\"result\",\"" + type.toString() + "\"}");

            sic.getOos().writeObject(um);
        }

        if (obj instanceof StudentMessage) { //如果发送的是StudentMessage, 对学员信息进行增删改查

            StudentMessage sm = (StudentMessage) obj;
            System.out.println("接收到的添加学员消息是:" + sm);

            String type = sm.getType();
            switch (type) {
                case "add": //增加一个学员
                    addStudent(sm);
                    break;
                case "select": //查询一个学员信息
                    selectStudent(sm);
                    break;
                case "delete": //删除一个学员
                    deleteStudent(sm);
                    break;
                case "modify": // 修改一个学员
                    modifyStudent(sm);
                    break;
                default:
                    System.out.println("消息类型错误");
                    break;
            }

        }
    }

    /**
     * 修改学员信息
     * @param sm
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void modifyStudent(StudentMessage sm) throws IOException, ClassNotFoundException {

        boolean isSuccess = ssm.modifyStudent(sm.getStudent());

        if (isSuccess) {
            sm.setType("success");
        } else {
            sm.setType("fail");
        }

        sic.getOos().writeObject(sm);
        System.out.println("服务端发送消息成功!" + sm);
    }

    /**
     * 删除学员信息
     * @param sm
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void deleteStudent(StudentMessage sm) throws IOException, ClassNotFoundException {

        boolean isSuccess = ssm.deleteStudent(sm.getStudent().getId());

        if (isSuccess) {
            sm.setType("success");
        } else {
            sm.setType("fail");
        }

        sic.getOos().writeObject(sm);
        System.out.println("服务端发送消息成功!" + sm);
    }

    /**
     * 增加一个学员
     * @param sm
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void addStudent(StudentMessage sm) throws IOException, ClassNotFoundException {

        boolean flag = ssm.addStudent(sm.getStudent());
        if (flag) {
            sm.setType("success");
        } else {
            sm.setType("fail");
        }

        sic.getOos().writeObject(sm);
        System.out.println("服务端发送消息成功!" + sm);
    }

    /**
     * 根据id查找学员信息
     * @param sm
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void selectStudent(StudentMessage sm) throws IOException, ClassNotFoundException {

        Student student = ssm.selectStudent(sm.getStudent().getId());
        if (null != student) {
            sm.setType("success");
            sm.setStudent(student);
        } else {
            sm.setType("fail");
        }

        sic.getOos().writeObject(sm);
        System.out.println("服务端发送消息成功!" + sm);
    }
}
